package clientView;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import clientMain.Main;
import clientUtilGUI.KeyValue;
import clientUtilGUI.UtilityGUI;

public class Employee_ConditionList extends JDialog implements ActionListener {
	private JPanel panel;
	private JLabel lblTitle;
	private JLabel lblDepChooser;
	private JComboBox cbbDep;
	private JPanel panelBtn;
	private JButton btnOk;
	private JButton btnCancel;

	public Employee_ConditionList(Main main) {
		super(main, UtilityGUI.tabEmDepList, true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		int height = 150;
	    int width = 300;
	    setSize(width, height);
	    setLocation((UtilityGUI.screenSize.width-width)/2, (UtilityGUI.screenSize.height-height)/2);
		setResizable(false);
		getContentPane().add(initComponent());
	}
	
	public void clear(){
		setVisible(false);
		getRootPane().removeAll();
		removeAll();
		dispose();
	}
	
	private JPanel initComponent() {
		panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0, 0};
		gbl_panel.rowHeights = new int[]{0, 0, 0};
		gbl_panel.columnWeights = new double[]{0.0, 1.0};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 1.0};
		panel.setLayout(gbl_panel);
		
		lblTitle = new JLabel("Xem Danh Sách Theo");
		lblTitle.setFont(UtilityGUI.fontHeader2);
		lblTitle.setForeground(UtilityGUI.colorHeader2);
		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.gridwidth = 2;
		gbc_lblTitle.insets = new Insets(10, 10, 10, 10);
		gbc_lblTitle.gridx = 0;
		gbc_lblTitle.gridy = 0;
		panel.add(lblTitle, gbc_lblTitle);
		
		lblDepChooser = new JLabel("Lựa Chọn :");
		lblDepChooser.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblDepChooser = new GridBagConstraints();
		gbc_lblDepChooser.anchor = GridBagConstraints.WEST;
		gbc_lblDepChooser.insets = new Insets(0, 10, 5, 5);
		gbc_lblDepChooser.gridx = 0;
		gbc_lblDepChooser.gridy = 1;
		panel.add(lblDepChooser, gbc_lblDepChooser);
		
		cbbDep = UtilityGUI.getComboBox("Department", 0,"");
		cbbDep.removeItemAt(0);
		cbbDep.addItem("Toàn Công Ty");
		cbbDep.setSelectedItem("Toàn Công Ty");
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 5, 10);
		gbc_comboBox.fill = GridBagConstraints.BOTH;
		gbc_comboBox.gridx = 1;
		gbc_comboBox.gridy = 1;
		panel.add(cbbDep, gbc_comboBox);
		
		panelBtn = new JPanel();
		panelBtn.setLayout(new FlowLayout(FlowLayout.RIGHT));
		GridBagConstraints gbc_panelBtn = new GridBagConstraints();
		gbc_panelBtn.insets = new Insets(3, 0, 10, 10);
		gbc_panelBtn.fill = GridBagConstraints.BOTH;
		gbc_panelBtn.gridx = 1;
		gbc_panelBtn.gridy = 2;
		panel.add(panelBtn, gbc_panelBtn);
		
		btnOk = new JButton(UtilityGUI.btnOk);
		btnOk.setFont(UtilityGUI.fontBtnForm);
		btnOk.addActionListener(this);
		panelBtn.add(btnOk);
		
		btnCancel = new JButton(UtilityGUI.btnCancel);
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		btnCancel.addActionListener(this);
		panelBtn.add(btnCancel);
		
		return panel;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnOk){
			if(cbbDep.getSelectedItem() == "Toàn Công Ty"){
				int index = Main.jTabbedPane.positionTab(UtilityGUI.tabEmComList);
				clear();
				if(index == -1){
					Main.jTabbedPane.addTab(new EmCom_List(1), UtilityGUI.tabEmComList);
					Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
				}else{
				 	Main.jTabbedPane.setSelectedIndex(index);
				 	return;
				}
			}else{
				int depId;
				String depName;
				depId = Integer.parseInt(((KeyValue)cbbDep.getSelectedItem()).getKey());
				depName = ((KeyValue)cbbDep.getSelectedItem()).getValue();
				clear();
				int index = Main.jTabbedPane.positionTab(UtilityGUI.tabEmDepList);
				if(index == -1){
					Main.jTabbedPane.addTab(new EmDep_List(depId, depName, 1), UtilityGUI.tabEmDepList);
					Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
				}else{
					Main.jTabbedPane.removeTabAt(index);
					Main.jTabbedPane.addTab(new EmDep_List(depId, depName, 1), UtilityGUI.tabEmDepList, index);
					Main.jTabbedPane.setSelectedIndex(index);
				 	return;
				}
			}
		}else if(e.getSource() == btnCancel){
			clear();
		}
	}
}
